rm(list=ls())

load("TSCS_data.RData")

# Figure E.10

panel %>% select(country_name,year,downturn,antipluralism_gov_seat_share_lag,citizen_support_lag,
                 polarization,clientelism_gov_seat_share,region_citizen_support,region_antipluralism_gov_seat_share,
                 region_liberal_democracy,region_liberal_democracy,democratic_stock,GDP_capita,life_expectancy) %>% 
  mutate(downturn_lag_1 = lag(downturn, 1),
         downturn_lag_2 = lag(downturn, 2),
         polarization_lag = lag(polarization, 1),
         clientelism_gov_seat_share_lag = lag(clientelism_gov_seat_share, 1),
         region_citizen_support_lag = lag(region_citizen_support, 1),
         region_antipluralism_gov_seat_share_lag = lag(region_antipluralism_gov_seat_share, 1),
         region_liberal_democracy_lag = lag(polarization_lag, 1),
         region_liberal_democracy = lag(region_liberal_democracy, 1),
         democratic_stock_lag = lag(democratic_stock, 1),
         GDP_capita_lag = lag(GDP_capita, 1),
         life_expectancy_lag = lag(life_expectancy, 1)) %>%
  drop_na(downturn,citizen_support_lag,antipluralism_gov_seat_share_lag) %>% 
  as.data.frame() -> 
  panel_binning

binning <- interflex(Y = "downturn",
                     D = "antipluralism_gov_seat_share_lag", 
                     X = "citizen_support_lag", 
                     Z = c("polarization_lag","clientelism_gov_seat_share_lag","region_citizen_support_lag",
                           "region_antipluralism_gov_seat_share_lag","region_liberal_democracy_lag",
                           "region_liberal_democracy_lag","democratic_stock_lag","GDP_capita_lag","life_expectancy_lag"),
                     data = panel_binning,
                     estimator = "binning",
                     FE = c("country_name", "year"), 
                     cl = "country_name",
                     na.rm = TRUE,
                     Ylabel = "Downturn",
                     Dlabel = "Anti-pluralist Government",
                     Xlabel = "Citizen Support",
                     theme.bw = T)

kernel <- interflex(Y = "downturn",
                    D = "antipluralism_gov_seat_share_lag", 
                    X = "citizen_support_lag", 
                    Z = c("polarization_lag","clientelism_gov_seat_share_lag","region_citizen_support_lag",
                          "region_antipluralism_gov_seat_share_lag","region_liberal_democracy_lag",
                          "region_liberal_democracy_lag","democratic_stock_lag","GDP_capita_lag","life_expectancy_lag"),
                    data = panel_binning,
                    estimator = "kernel",
                    FE = c("country_name", "year"), 
                    cl = "country_name",
                    na.rm = TRUE,
                    Ylabel = "Downturn",
                    Dlabel = "Anti-pluralist Government",
                    Xlabel = "Citizen Support",
                    theme.bw = T)

plot(binning)
binning_plot <- recordPlot()

pdf(file = "fig_e10a.pdf",width = 8,height = 6)

binning_plot

dev.off()

plot(kernel)
kernel_plot <- recordPlot()

pdf(file = "fig_e10b.pdf",width = 8,height = 6)

kernel_plot

dev.off()
